package com.cci.kangdao.service.impl;

import com.cci.kangdao.dao.ServiceToolsServiceDao;
import com.cci.kangdao.dao.model.SMNT;
import com.cci.kangdao.dao.model.ServiceToolT;
import com.cci.kangdao.service.PropertiesUtils;
import com.cci.kangdao.service.ServiceToolsService;
import com.cci.kangdao.utilTool.AESTool;
import com.cci.kangdao.utilTool.ApiErrorCode;
import com.cci.kangdao.utilTool.ParameterTool;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections.MapUtils;
import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("serviceToolsService")
public class ServiceToolsServiceImpl implements ServiceToolsService {
	private Logger log = Logger.getLogger(this.getClass().getName());

	@Autowired
	private ServiceToolsServiceDao serviceToolsServiceDao;

	@Resource
	private PropertiesUtils propertiesUtils;

	/**
	 * App用户机型获取SMN接口
	 */
	@Override
	public ModelAndView getSMNByEngintype(Map<String, Object> map) {
		try {
			HttpServletResponse response = (HttpServletResponse) map.get("response");
			HttpServletRequest request = (HttpServletRequest) map.get("request");
			JSONObject obj = new JSONObject();

			if (map.get("engineTypeID") == null || map.get("engineTypeID").toString().equals("")) {
				obj.put("status", -5);
				obj.put("msg", ApiErrorCode.getInstance().getErrorMsg(-5));

				// 返回客户
				ParameterTool.writeResponse(response, obj.toString());
				obj = null;
				return null;

			}

			int page = 1;
			if (map.get("page") != null) {
				page = MapUtils.getInteger(map, "page", 1);
			}
			int pagesize = Integer.valueOf(this.propertiesUtils.getPropertiesValue("pagesize"));

//			String engineTypeID = (String) map.get("engineTypeID");
//			// 解密
//			engineTypeID = AESTool.decryptString(engineTypeID);
//			map.put("engineTypeID", engineTypeID);
			PageHelper.startPage(page, pagesize);
			// 查询smn
			List<SMNT> byEngintype = serviceToolsServiceDao.getSMNByEngintype(map);
			// 将内容返回给客户
			obj.put("status", 0);
			obj.put("msg", "");
			JSONObject content;
			JSONArray jsonArray = new JSONArray();
			if (byEngintype != null) {
				for (SMNT smnt : byEngintype) {
					content = new JSONObject();
					content.put("id", AESTool.encryptString(smnt.getID().toString()));
					content.put("SMNname", smnt.getSMNname());
					jsonArray.put(content);
				}

			}
			obj.put("SMSes", jsonArray);
			ParameterTool.writeResponse(response, obj.toString());
			obj = null;
		} catch (Exception e) {
			// TODO: handle exception
			log.error(e.getMessage(), e);
		}
		return null;
	}

	/**
	 * App用户查询服务工具接口
	 */
	@Override
	public ModelAndView searchServiceTool(Map<String, Object> map) {
		try {
			HttpServletResponse response = (HttpServletResponse) map.get("response");
			HttpServletRequest request = (HttpServletRequest) map.get("request");
			JSONObject obj = new JSONObject();

			// 获取参数
			if (map.get("searchTxt") == null) {
				obj.put("status", -5);
				obj.put("msg", ApiErrorCode.getInstance().getErrorMsg(-5));

				// 返回客户
				ParameterTool.writeResponse(response, obj.toString());
				obj = null;
				return null;
			}
			if(map.get("searchTxt") != null && !map.get("searchTxt").toString().equals("")){
				map.put("searchTxt", "%"+map.get("searchTxt")+"%");
			}

			int page = 1;
			if (map.get("page") != null) {
				page = MapUtils.getInteger(map, "page", 1);
			}
			int pagesize = Integer.valueOf(this.propertiesUtils.getPropertiesValue("pagesize"));

			String engineTypeID = "";
			if (map.get("engineTypeID") != null && !map.get("engineTypeID").toString().equals("")) {
				engineTypeID = (String) map.get("engineTypeID");
				map.put("engineTypeID", engineTypeID);
				
			}

			List<String> SMNID = new ArrayList<>();
			if (map.get("SMNID") != null && !map.get("SMNID").toString().equals("")) {
				SMNID.add((String) map.get("SMNID"));
				map.put("SMNID", SMNID);
				
			}

			// 将内容返回给客户
			obj.put("status", 0);
			obj.put("msg", "");
			JSONObject content = null;
			JSONArray jsonArray = new JSONArray();
			if (engineTypeID.trim().equals("") && String.valueOf(map.get("SMNID")).trim().equals("") && map.get("searchTxt").toString().trim().equals("")) {
				PageHelper.startPage(page, pagesize);
				List<ServiceToolT> serviceTool = serviceToolsServiceDao.searchServiceTool(map);
				if (serviceTool != null) {
					for (ServiceToolT serviceToolT : serviceTool) {
						content = new JSONObject();
						content.put("id", AESTool.encryptString(serviceToolT.getID().toString()));
						content.put("PnNode", serviceToolT.getPnNode());
						content.put("toolName", serviceToolT.getToolName());
						jsonArray.put(content);
					}
				}
				obj.put("serviceTools", jsonArray);
				ParameterTool.writeResponse(response, obj.toString());
				obj = null;
				return null;
			} else {
				/**
				 * 全部机型
				 */
				if(map.get("SMNID").toString().equals("") && map.get("engineTypeID") !=null) {
					List<SMNT> smnByEngintype = serviceToolsServiceDao.getSMNByEngintype(map);
					if (smnByEngintype != null) {
//						for (SMNT smnt : smnByEngintype) {
//							SMNID +=smnt.getID()+",";
//						}
//						if(SMNID.endsWith(",")){
//							SMNID=SMNID.substring(0, SMNID.length()-1);
//						}
						List<Long> smnids = smnByEngintype.stream().map(SMNT::getID).collect(Collectors.toList());
						map.put("SMNID", smnids);
					}
				}
				// 筛选查询
				PageHelper.startPage(page, pagesize);
				List<ServiceToolT> serviceToolDatas = serviceToolsServiceDao.searchScreenServiceTool(map);
				if (serviceToolDatas != null) {
					for (ServiceToolT serviceToolTs : serviceToolDatas) {
						content = new JSONObject();
						content.put("id", AESTool.encryptString(serviceToolTs.getID().toString()));
						content.put("PnNode", serviceToolTs.getPnNode());
						content.put("toolName", serviceToolTs.getToolName());
						jsonArray.put(content);
					}
				}
				obj.put("serviceTools", jsonArray);
				ParameterTool.writeResponse(response, obj.toString());
				obj = null;
			}
		} catch (Exception e) {
			// TODO: handle exception
			log.error(e.getMessage(), e);
		}
		return null;
	}

	/**
	 * App用户获取服务工具详细接口
	 */
	@Override
	public ModelAndView getServiceToolDetail(Map<String, Object> map) {
		try {
			HttpServletResponse response = (HttpServletResponse) map.get("response");
			HttpServletRequest request = (HttpServletRequest) map.get("request");
			JSONObject obj = new JSONObject();

			if (map.get("serviceToolID") == null || map.get("serviceToolID").toString().equals("")) {
				obj.put("status", -5);
				obj.put("msg", ApiErrorCode.getInstance().getErrorMsg(-5));

				// 返回客户
				ParameterTool.writeResponse(response, obj.toString());
				obj = null;
				return null;
			}
//			int page = 1;
//			if (map.get("page") != null) {
//				page = MapUtils.getInteger(map, "page", 1);
//			}
//			int pagesize = Integer.valueOf(this.propertiesUtils.getPropertiesValue("pagesize"));

			String serviceToolID = (String) map.get("serviceToolID");
			// 解密
			serviceToolID = AESTool.decryptString(serviceToolID);
			map.put("serviceToolID", serviceToolID);
//			PageHelper.startPage(page, pagesize);
			
			//记录阅读次数
			try {
				serviceToolsServiceDao.updateReadNum(map);
			} catch(Exception e) {
				log.error("阅读次数更新失败！服务工具ID:" + serviceToolID);
			}
			
			// 查询出主工具信息
			List<ServiceToolT> serviceTool = serviceToolsServiceDao.getServiceTool(map);
			// 将内容返回给客户
			obj.put("status", 0);
			obj.put("msg", "");
			JSONObject content = null;
			JSONArray jsonArray = new JSONArray();
			if (serviceTool != null) {
				if(serviceTool.size()>0){
				ServiceToolT toolT = serviceTool.get(0);
				obj.put("PnNode", toolT.getPnNode());
				obj.put("toolName", toolT.getToolName());
				obj.put("description", toolT.getDescription());
				obj.put("descImgs", toolT.getDescImgs());
				obj.put("toolImgs", toolT.getToolImgs());
				obj.put("matchImgs", toolT.getMatchImgs());
				}
			}
			// 查询副工具
			List<ServiceToolT> toolsDatas = serviceToolsServiceDao.getSideToolsDatas(map);
			if (toolsDatas != null) {
				for (ServiceToolT serviceToolT : toolsDatas) {
					content = new JSONObject();
					content.put("id", AESTool.encryptString(serviceToolT.getID().toString()));
					content.put("PnNode", serviceToolT.getPnNode());
					content.put("toolName", serviceToolT.getToolName());
					jsonArray.put(content);
				}
			}
			obj.put("serviceTools", jsonArray);
			// 查询出SMN码
			List<SMNT> smntDatas = serviceToolsServiceDao.getSMNTDatas(map);
			JSONObject contentSmn = null;
			JSONArray jsonArraySmn = new JSONArray();
			if (smntDatas != null) {
				for (SMNT smnt : smntDatas) {
					contentSmn = new JSONObject();
					contentSmn.put("id", AESTool.encryptString(smnt.getID().toString()));
					contentSmn.put("SMNname", smnt.getSMNname());
					contentSmn.put("engineTypeID", smnt.getEngineTypeID());
					jsonArraySmn.put(contentSmn);
				}
			}
			obj.put("SMSes", jsonArraySmn);
			ParameterTool.writeResponse(response, obj.toString());
			obj = null;
			return null;
		} catch (Exception e) {
			// TODO: handle exception
			log.error(e.getMessage(), e);
		}
		return null;
	}

}
